library(monocle3)
library(tidySingleCellExperiment)
library(tidyverse)

sobj <- read_rds('mission/ye_AH-cOME/ah9.rds')

ah9.mtx <- sobj@assays$RNA$counts

ah9.mtx[1:5,1:4]

cds.ah9 <- new_cell_data_set(ah9.mtx,
                             cell_metadata = sobj@meta.data)

rowData(cds.ah9)$gene_short_name <- row.names(rowData(cds.ah9))

cds.ah9 <- preprocess_cds(cds.ah9)

cds.ah9

cds.ah9 <- cds.ah9 |>
  align_cds(alignment_group = "id")

cds.ah9 <- reduce_dimension(cds.ah9)

cds.ah9 <- cluster_cells(cds.ah9, verbose = T)

plot_cells(cds.ah9, color_cells_by = 'manual_wang', group_label_size = 6)

plot_cells(cds.ah9, group_label_size = 6)

mnc.marker.ah9 <- cds.ah9 |>
  top_markers(cores = 4)

mnc.marker.ah9 |> DT::datatable()

mnc.tmarker.ah9 <- mnc.marker.ah9 |>
  as_tibble() |>
  slice_max(marker_score, by = cell_group) |>
  pull(gene_id)

cds.ah9 |>
  plot_genes_by_group(mnc.tmarker.ah9)

cds.ah9 <- cds.ah9 |>
  learn_graph(use_partition = F)

cds.ah9 |> plot_cells()

cds.ah9 <- order_cells(cds.ah9)

cds.ah9 |> plot_cells(color_cells_by = 'pseudotime')
